home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / game / role / ldmud-3.2-bin.lha / mud / doc / LPC / foreach < prev    next >
Text File  |  2001-04-06  |  3KB  |  72 lines

  1. NAME
  2.         foreach
  3.  
  4. SYNTAX
  5.         foreach (<var> : <expr>) <statement>;
  6.         foreach (<var>, <var2>, ... ,<varN> : <expr>) <statement>;
  7.  
  8.         /* MudOS compatibility only - not for new code: */
  9.         foreach (<var> in <expr>) <statement>;
  10.         foreach (<var>, <var2>, ... ,<varN> in <expr>) <statement>;
  11.  
  12. DESCRIPTION
  13.         <expr> is evaluated and has to yield an array, a string or a mapping.
  14.         The values of <expr> (in case of the string, the integer values
  15.         of the characters) are then assigned one after another to <var>
  16.         and <statement> is execute for every assignment.
  17.  
  18.         If <expr> is a mapping, the keys are assigned to <var>, and
  19.         the values for each key are assigned in order to <var2>..<varN>.
  20.         If there are more values than variable, the extraneous values
  21.         are ignored.
  22.  
  23.         If there are more variables than necessary, the unneeded ones
  24.         are not changed.
  25.  
  26.         Every <var> specification can declare a new local variable, whose
  27.         scope is the whole foreach() statement.
  28.  
  29.         A 'break' in the 'statement' will terminate the loop. A
  30.         'continue' will continue the execution from the beginning of
  31.         the loop.
  32.  
  33.  
  34.         WHAT HAPPENS IF <expr> IS CHANGED IN THE LOOP?
  35.  
  36.         If <expr> yields an array:
  37.          - assignments to single array elements or to array ranges effect
  38.            the values assigned to the variable:
  39.              a = ({1, 2, 3})
  40.              foreach(x : a) { a[1..2] = ({4, 5}); write(x+" "); }
  41.            will write ("1 4 5 ").
  42.          - operations which implicitely copy the array (this includes
  43.            range assignments which change the size) don't have an effect
  44.            on the loop.
  45.  
  46.         If <expr> yields an mapping, the loop will run over the indices
  47.         the mapping had at the begin of the loop. Deleted indices are silently
  48.         skipped, new indices ignored, but changes of the data of existing
  49.         indices are acknowledged.
  50.  
  51.         If <expr> yields a string, it can't be changed anyway.
  52.  
  53.  
  54. WARNING 
  55.  
  56.         The additional syntax forms using "in" as keyword are meant
  57.         to make re-engineering of MudOS objects easier. Do not use them
  58.         for newly written code, as they may not be available in future.
  59.  
  60.  
  61. EXAMPLES
  62.         // Call quit() in all interactive users
  63.         foreach(o : users()) o->quit();
  64.         foreach(object o : users()) o->quit();
  65.  
  66.         // Print the contents of a mapping <m>
  67.         foreach(key, value : m) printf("%O:%O\n", key, value);
  68.         foreach(mixed key, mixed value : m) printf("%O:%O\n", key, value);
  69.  
  70. SEE ALSO
  71.         for(LPC)
  72.